package com.newly.center.sys.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.newly.api.sys.feign.DomainClient;
import com.newly.center.sys.service.domain.api.IDomainService;
import com.newly.common.base.constants.AuthConstant;
import com.newly.common.base.constants.ServerUriConstant;
import com.newly.common.base.entity.R;
import com.newly.common.base.entity.sys.domain.dto.DomainDto;
import com.newly.common.base.entity.sys.domain.po.DomainPo;
import com.newly.common.base.entity.sys.domain.vo.DomainVo;
import com.newly.common.core.utils.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: newly-parent
 * @remark: 区域
 * @author: guan
 * @create: 2022-01-14 16:03
 */
@RestController
@RequestMapping(ServerUriConstant.NEWLY_SYS_DOMAIN)
public class DomainApi implements DomainClient {

    @Autowired
    private IDomainService domainService;

    @Autowired
    private RequestUtils requestUtils;

    @Override
    public R create(DomainDto domainDto) {
        boolean result = false;
        try {
            result = domainService.save(domainDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R change(DomainDto domainDto) {
        boolean result = false;
        try {
            result = domainService.modifyById(domainDto);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }

    @Override
    public R deleteByIds(List<Integer> domainIds) {
        boolean result = false;
        try {
            result = domainService.removeByIds(domainIds);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(result);
    }


    @Override
    public R listInAuth(String keyword, String fromTime, String toTime) {
        List<DomainVo> domainVos = null;
        try {
            List<DomainPo> domainPos = domainService.listInAuth(keyword, fromTime, toTime);
            if (!ObjectUtil.isEmpty(domainPos)) {
                domainVos = BeanUtil.copyToList(domainPos, DomainVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(domainVos);
    }

    @Override
    public R queryById(Integer domainId) {
        DomainVo domainVo = null;
        try {
            DomainPo domainPo = domainService.getByIdInAuth(domainId);
            if (!ObjectUtil.isEmpty(domainPo)) {
                domainVo = BeanUtil.copyProperties(domainPo, DomainVo.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(domainVo);
    }

    @Override
    public R queryEnable() {
        AuthConstant.Enable[] values = null;
        try {
            values = AuthConstant.Enable.values();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
        return R.ok(values);
    }
}
